/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kn.cstf2.transition.Shared;

import com.opensymphony.xwork2.ActionContext;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;
import kn.cstf2.assign.Client.MailSender;
import kn.cstf2.global.Utils.CstF2Exception;
import kn.cstf2.transition.Bean.*;
import kn.cstf2.transition.Server.TransitionServer;
import kn.mx.cstf2.dto.UsuarioDto;

/**
 *
 * @author user1
 */
public class TransitionShared {

    TransitionServer transitionServer = new TransitionServer();
    private TransitionBean transitionBean = new TransitionBean();

    public TransitionShared() {
        UsuarioDto usuario = new UsuarioDto();
        ActionContext contexto = ActionContext.getContext();
        Map<String, Object> sesion = contexto.getSession();
        usuario = (UsuarioDto) sesion.get("usuarioSession");

        //ActionContext contexto = ActionContext.getContext();
        Map<String, Object> sesionKnkey = contexto.getSession();
        transitionBean = (TransitionBean) sesionKnkey.get("Knkey");
    }

    public TransitionShared(String nada) {
    }

    public TransitionBean GetTransitionBeanIdKnkey(TransitionBean transitionBean) {

        // TransitionBean transitionBean = new TransitionBean();
        if (transitionBean != null) {
            transitionBean = transitionServer.obtenerTransitionBeanIdKnkeyByKnkey(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return transitionBean;
    }

    public String GetStatusSelected(Integer knkey) {

        TransitionBean transitionBean = new TransitionBean();
        String status = "";
        if (knkey != null) {
            transitionBean = transitionServer.obtenerStatus(knkey);
            status = transitionBean.getStatus();
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return status;
    }

    public String GetStatusImplementationSelected(Integer knkey) {

        TransitionBean transitionBean = new TransitionBean();
        String status = "";
        if (knkey != null) {
            transitionBean = transitionServer.obtenerStatusImplementation(knkey);
            status = transitionBean.getStatusimple();
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return status;
    }

    public T_clientesDataFromStkBean GetDataFromSTK(TransitionBean transitionBean) {
        T_clientesDataFromStkBean t_clientesDataFromStkBean = new T_clientesDataFromStkBean();
        if (t_clientesDataFromStkBean != null) {
            t_clientesDataFromStkBean = transitionServer.obtenerDataFromTClientesSTK(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return t_clientesDataFromStkBean;
    }

    public AsignIfownerStkBean GetIfOwnerFromSTK(TransitionBean transitionBean) {

        AsignIfownerStkBean asignIfownerStkBean = new AsignIfownerStkBean();
        if (asignIfownerStkBean != null) {
            asignIfownerStkBean = transitionServer.obtenerIfOwnerFromSTK(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return asignIfownerStkBean;
    }

    public TransitionBean GetDataFromTransition(TransitionBean transitionBean) {

        //TransitionBean transitionBean=new TransitionBean();
        if (transitionBean != null) {
            transitionBean = transitionServer.obtenerDataFromTransition(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return transitionBean;
    }

    public TransitionBean GetDataFromTransitionIdKnkey(TransitionBean transitionBean) {

        //TransitionBean transitionBean=new TransitionBean();
        if (transitionBean != null) {
            transitionBean = transitionServer.obtenerDataFromTransitionIdKnkey(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return transitionBean;
    }

    public DocumentsBean GetDocumentName(TransitionBean transitionBean) {

        DocumentsBean documentsBean = new DocumentsBean();
        if (documentsBean != null) {
            documentsBean = transitionServer.obtenerDocumentName(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return documentsBean;
    }

    public DocumentsBean GetDocumentId(DocumentsBean documentsBean) {

        if (documentsBean != null) {
            documentsBean = transitionServer.obtenerDocumentId(documentsBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return documentsBean;
    }

    public JoindocumenttransitionBean GetJoindocumenttransitionData(TransitionBean transitionBean) {

        JoindocumenttransitionBean joindocumenttransitionBean = new JoindocumenttransitionBean();
        if (joindocumenttransitionBean != null) {
            joindocumenttransitionBean = transitionServer.obtenerJoindocumenttransitionData(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return joindocumenttransitionBean;
    }

    public String GetJoindocumenttransitionDetail(Integer iddocument, Integer idtransition) {

        String detailsd = "";
        if (idtransition != null) {
            detailsd = transitionServer.obtenerJoindocumenttransitionDetail(iddocument, idtransition);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return detailsd;
    }

    public PersonalBean GetDataPersonalTransitionBean(TransitionBean transitionBean) {

        PersonalBean personalBean = new PersonalBean();
        if (personalBean != null) {
            personalBean = transitionServer.obtenerDataPersonal(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return personalBean;
    }

    public PersonalBean GetDataPersonal(TransitionBean transitionBean) {

        PersonalBean personalBean = new PersonalBean();
        if (personalBean != null) {
            personalBean = transitionServer.obtenerDataPersonal(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return personalBean;
    }

    public PersonalBean GetDataPersonalByName(String name) {

        PersonalBean personalBean = new PersonalBean();
        if (personalBean != null) {
            personalBean = transitionServer.obtenerDataPersonalByName(name);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return personalBean;
    }

    public ArrayList<PersonalBean> GetDataPersonalByKnkey(Integer knkey) {

        PersonalBean personalBean = new PersonalBean();
        ArrayList<PersonalBean> PersonalListbyKnkeyTransition = new ArrayList<PersonalBean>();

        if (personalBean != null) {
            PersonalListbyKnkeyTransition = transitionServer.obtenerDataPersonalByKnkey(knkey);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return PersonalListbyKnkeyTransition;
    }

    public ArrayList<PersonalBean> GetDataPersonalByIdPersonal(Integer idp) {

        PersonalBean personalBean = new PersonalBean();
        ArrayList<PersonalBean> PersonalListbyKnkeyTransition = new ArrayList<PersonalBean>();

        if (personalBean != null) {
            PersonalListbyKnkeyTransition = transitionServer.obtenerDataPersonalByIdPersonal(idp);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return PersonalListbyKnkeyTransition;
    }

    public PersonalBean GetDataPersonalById(Integer idp) {

        PersonalBean personalBean = new PersonalBean();

        if (personalBean != null) {
            personalBean = transitionServer.obtenerDataPersonalById(idp);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return personalBean;
    }

    public PersonalBean GetDataPersonalByEmail(String mail) {

        PersonalBean personalBean = new PersonalBean();
        if (personalBean != null) {
            personalBean = transitionServer.obtenerDataPersonalByEmail(mail);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return personalBean;
    }

    public ArrayList<String> GetListaNombreDePersonal() {
        ArrayList<String> name;

        name = transitionServer.selectListaNombreDePersonal();

        return name;
    }

//   public ArrayList<PersonalBean> GetListaNombreDePersonalByKnkeyTransition (Integer knkey){
//     ArrayList<PersonalBean>name;
//               
//            name = transitionServer.selectListaNombreDePersonalByKnkeyTransition(knkey);
//        
//    return name;
//   }   
    public JoindocumenttransitionBean GetIDsFromJoindocumenttransition(TransitionBean transitionBean, DocumentsBean documentsBean) {

        JoindocumenttransitionBean joindocumenttransitionBean = new JoindocumenttransitionBean();
        if (joindocumenttransitionBean != null) {
            joindocumenttransitionBean = transitionServer.obtenerIDsFromJoindocumenttransition(transitionBean, documentsBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return joindocumenttransitionBean;
    }

    public JoindocumenttransitionBean SetRelacionEnJoindocumenttransition(TransitionBean transitionBean, DocumentsBean documentsBean) {

        JoindocumenttransitionBean joindocumenttransitionBean = new JoindocumenttransitionBean();
        if (joindocumenttransitionBean != null) {
            joindocumenttransitionBean = transitionServer.insertarRelacionEnJoindocumenttransition(transitionBean, documentsBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return joindocumenttransitionBean;
    }

    public JoindocumenttransitionBean UpdateVoBoInJoindocumenttransition(JoindocumenttransitionBean joindocumenttransitionBean, String fecha) {


        if (joindocumenttransitionBean != null) {
            joindocumenttransitionBean = transitionServer.actualizarVoBoEnJoindocumenttransition(joindocumenttransitionBean, fecha);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return joindocumenttransitionBean;
    }

    public JoindocumenttransitionBean GetVoBoInJoindocumenttransition(Integer idtransition) {

        JoindocumenttransitionBean joindocumenttransitionBean = new JoindocumenttransitionBean();

        if (idtransition != null) {
            joindocumenttransitionBean = transitionServer.selectVoBoEnJoindocumenttransition(idtransition);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return joindocumenttransitionBean;
    }

    public TransitionBean UpdateLogEnTransition(TransitionBean transitionBean) {

        if (transitionBean != null) {
            transitionBean = transitionServer.actualizarLogEnTransition(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return transitionBean;
    }

    public TransitionBean UpdateStatusEnTransition(TransitionBean transitionBean) {

        if (transitionBean != null) {
            transitionBean = transitionServer.actualizarStatusEnTransition(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return transitionBean;
    }

    public TransitionBean UpdateStatusEnImplementation(TransitionBean transitionBean) {

        if (transitionBean != null) {
            transitionBean = transitionServer.actualizarStatusEnImplementation(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return transitionBean;
    }

    public void InsertDataPersonal(PersonalBean personalBean) {

        if (personalBean != null) {
            transitionServer.insertarDataPersonal(personalBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        //return personalBean;
    }

    public void UpdateDetailsInJoindocumenttransition(String detailsnum, Integer documentid, Integer transitionId) {

        if (transitionId != null) {
            transitionServer.actualizarDetailsEnJoindocumenttransition(detailsnum, documentid, transitionId);
        } else {
            //aqui debere validad si es nulo para ocupar el metodo insert
            throw new CstF2Exception("El Usuario es null");
        }
        //return personalBean;
    }

    public void InsertDetailsInJoindocumenttransition(PersonalBean personalBean) {

        if (personalBean != null) {
            transitionServer.insertarDataPersonal(personalBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        //return personalBean;
    }

    public void SetJoinPersonTransition(PersonalBean personalBean, TransitionBean transitionBean) {
        //JoinPersonTransitionBean   joinPersonTransitionBean = new   JoinPersonTransitionBean();
        if (personalBean != null) {
            transitionServer.insertarJoinPersonTransition(personalBean, transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        //return joinPersonTransitionBean;
    }

    public void newTransition(int KnKey, String companyName) {
        transitionServer.newTransition(KnKey);
        sendEmail("" + KnKey, companyName);
    }

    private void sendEmail(String knkeys, String companyName) {
        try {
            MailSender mailSender = new MailSender();
            ArrayList<String> direcciones = transitionServer.getSuperUsers();
            StringBuilder mensaje = new StringBuilder();
            mensaje.append("Transition iniciated KNKEY: ");
            mensaje.append(knkeys);
            mensaje.append(" COMPANYNAME: ");
            mensaje.append(companyName);
            
            System.out.println(mensaje);

            mailSender.sendEmail(direcciones, "Assigned knkey confirmation", mensaje);

        } catch (CstF2Exception ex) {
        }

    }

    public boolean transitionExists(int knkey) {
        return transitionServer.transitionExists(knkey);
    }

    public void updateUploaded(String file, int knkey) {
        transitionServer.updateUploaded(file, knkey);
    }

    public void updateUploaded(String file, int knkey, String value) {
        transitionServer.updateUploaded(file, knkey, value);
    }

    public boolean fileUploaded(String file, int knkey) {
        return transitionServer.fileUploaded(file, knkey);
    }

    public void DeletePersonalById(PersonalBean personalBean) {
        if (personalBean != null) {
            transitionServer.eliminarPersonalById(personalBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
    }

    public void DeleteRelationJoinusertransitionByIdPerson(Integer idperson, Integer idtransition) {
        if (idperson != null) {
            transitionServer.eliminarRelacionJoinPersontransitionByIdPerson(idperson, idtransition);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
    }

    public Integer VerificarCountJoinPersontransition(JoinPersonTransitionBean joinPersonTransitionBean) {
        int count = 0;
        if (joinPersonTransitionBean != null) {
            count = transitionServer.verificarCountJoinusertransition(joinPersonTransitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return count;
    }

    public java.util.Date GetTheDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
        java.util.Date date = new java.util.Date();

        //dateFormat.format(datesql);
        System.out.println(dateFormat.format(date));

        return date;
    }

    public String GetTheDateTimeString() {
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
        java.util.Date date = new java.util.Date();
        java.sql.Date datesql = new java.sql.Date(date.getDate());
        //dateFormat.format(datesql);
        System.out.println(dateFormat.format(date));
        String fecha = datesql.toGMTString();
        return fecha;
    }

    public String GetTransitionLog(Integer knkey) {
        String Tlog;
        Tlog = transitionServer.obtenerTransitionLog(knkey);

        return Tlog;

    }

    public void UpdateVoBoCheckbox(JoindocumenttransitionBean joindocumenttransitionBean, int iddocument) {
        
        if (joindocumenttransitionBean != null) {
            if(transitionServer.checkCheckbox(joindocumenttransitionBean, iddocument)){
                transitionServer.updateCheckbox(joindocumenttransitionBean, iddocument);
            }
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
    }
}
